From 4345c2c20ec3eb5281e33bed6614751fcb369303 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Sun, 21 Nov 2010 19:56:51 +0000 Subject: [PATCH] Refactoring of *Field classes: * Made them all implement one common interface (might add more functions to it later) * Moved MySQLField to DatabaseMysql.php * Renamed nullable() to isNullable() * Removed maxLength() from: ** SQLiteField: makes no sense ** MySQLField: doesn't do what people may think, useless for this class' purpose of assisting querying the DB schema --- includes/AutoLoader.php | 3 +- includes/db/Database.php | 68 +++++++++----------------- includes/db/DatabaseIbm_db2.php | 4 +- includes/db/DatabaseMssql.php | 4 +- includes/db/DatabaseMysql.php | 50 +++++++++++++++++++ includes/db/DatabaseOracle.php | 4 +- includes/db/DatabasePostgres.php | 4 +- includes/db/DatabaseSqlite.php | 11 +---- includes/installer/MysqlUpdater.php | 2 +- includes/installer/PostgresUpdater.php | 4 +- 10 files changed, 87 insertions(+), 67 deletions(-) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 79148d9c70..bcaa0387fb 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -381,6 +381,7 @@ $wgAutoloadLocalClasses = array( 'DBQueryError' => 'includes/db/Database.php', 'DBUnexpectedError' => 'includes/db/Database.php', 'FakeResultWrapper' => 'includes/db/Database.php', + 'Field' => 'includes/db/Database.php', 'IBM_DB2Blob' => 'includes/db/DatabaseIbm_db2.php', 'LBFactory' => 'includes/db/LBFactory.php', 'LBFactory_Multi' => 'includes/db/LBFactory_Multi.php', @@ -389,7 +390,7 @@ $wgAutoloadLocalClasses = array( 'LoadBalancer' => 'includes/db/LoadBalancer.php', 'LoadMonitor' => 'includes/db/LoadMonitor.php', 'LoadMonitor_MySQL' => 'includes/db/LoadMonitor.php', - 'MySQLField' => 'includes/db/Database.php', + 'MySQLField' => 'includes/db/DatabaseMysql.php', 'MySQLMasterPos' => 'includes/db/DatabaseMysql.php', 'ORABlob' => 'includes/db/DatabaseOracle.php', 'ORAField' => 'includes/db/DatabaseOracle.php', diff --git a/includes/db/Database.php b/includes/db/Database.php index 9b80334aba..cf6d9dfa37 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2646,57 +2646,33 @@ class Blob { } /** - * Utility class. + * Base for all database-specific classes representing information about database fields * @ingroup Database */ -class MySQLField { - private $name, $tablename, $default, $max_length, $nullable, - $is_pk, $is_unique, $is_multiple, $is_key, $type; - - function __construct ( $info ) { - $this->name = $info->name; - $this->tablename = $info->table; - $this->default = $info->def; - $this->max_length = $info->max_length; - $this->nullable = !$info->not_null; - $this->is_pk = $info->primary_key; - $this->is_unique = $info->unique_key; - $this->is_multiple = $info->multiple_key; - $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple ); - $this->type = $info->type; - } - - function name() { - return $this->name; - } - - function tableName() { - return $this->tableName; - } - - function defaultValue() { - return $this->default; - } - - function maxLength() { - return $this->max_length; - } - - function nullable() { - return $this->nullable; - } +interface Field { + /** + * Field name + * @return string + */ + function name(); - function isKey() { - return $this->is_key; - } + /** + * Name of table this field belongs to + * @return string + */ + function tableName(); - function isMultipleKey() { - return $this->is_multiple; - } + /** + * Database type + * @return string + */ + function type(); - function type() { - return $this->type; - } + /** + * Whether this field can store NULL values + * @return bool + */ + function isNullable(); } /****************************************************************************** diff --git a/includes/db/DatabaseIbm_db2.php b/includes/db/DatabaseIbm_db2.php index 579581ca43..0105a23b7c 100644 --- a/includes/db/DatabaseIbm_db2.php +++ b/includes/db/DatabaseIbm_db2.php @@ -13,7 +13,7 @@ * This represents a column in a DB2 database * @ingroup Database */ -class IBM_DB2Field { +class IBM_DB2Field implements Field { private $name = ''; private $tablename = ''; private $type = ''; @@ -75,7 +75,7 @@ SQL; * Can column be null? * @return bool true or false */ - function nullable() { return $this->nullable; } + function isNullable() { return $this->nullable; } /** * How much can you fit in the column per row? * @return int length diff --git a/includes/db/DatabaseMssql.php b/includes/db/DatabaseMssql.php index 943c070f1c..4846563654 100644 --- a/includes/db/DatabaseMssql.php +++ b/includes/db/DatabaseMssql.php @@ -1051,7 +1051,7 @@ class DatabaseMssql extends DatabaseBase { * * @ingroup Database */ -class MssqlField { +class MssqlField implements Field { private $name, $tablename, $default, $max_length, $nullable, $type; function __construct ( $info ) { $this->name = $info['COLUMN_NAME']; @@ -1077,7 +1077,7 @@ class MssqlField { return $this->max_length; } - function nullable() { + function isNullable() { return $this->nullable; } diff --git a/includes/db/DatabaseMysql.php b/includes/db/DatabaseMysql.php index 07a4042ccd..39e8037a4b 100644 --- a/includes/db/DatabaseMysql.php +++ b/includes/db/DatabaseMysql.php @@ -503,6 +503,56 @@ class DatabaseMysql extends DatabaseBase { */ class Database extends DatabaseMysql {} +/** + * Utility class. + * @ingroup Database + */ +class MySQLField implements Field { + private $name, $tablename, $default, $max_length, $nullable, + $is_pk, $is_unique, $is_multiple, $is_key, $type; + + function __construct ( $info ) { + $this->name = $info->name; + $this->tablename = $info->table; + $this->default = $info->def; + $this->max_length = $info->max_length; + $this->nullable = !$info->not_null; + $this->is_pk = $info->primary_key; + $this->is_unique = $info->unique_key; + $this->is_multiple = $info->multiple_key; + $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple ); + $this->type = $info->type; + } + + function name() { + return $this->name; + } + + function tableName() { + return $this->tableName; + } + + function type() { + return $this->type; + } + + function isNullable() { + return $this->nullable; + } + + function defaultValue() { + return $this->default; + } + + function isKey() { + return $this->is_key; + } + + function isMultipleKey() { + return $this->is_multiple; + } +} + class MySQLMasterPos { var $file, $pos; diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php index b1f34f06fe..b258ca588b 100644 --- a/includes/db/DatabaseOracle.php +++ b/includes/db/DatabaseOracle.php @@ -124,7 +124,7 @@ class ORAResult { * Utility class. * @ingroup Database */ -class ORAField { +class ORAField implements Field { private $name, $tablename, $default, $max_length, $nullable, $is_pk, $is_unique, $is_multiple, $is_key, $type; @@ -157,7 +157,7 @@ class ORAField { return $this->max_length; } - function nullable() { + function isNullable() { return $this->nullable; } diff --git a/includes/db/DatabasePostgres.php b/includes/db/DatabasePostgres.php index ec0e663ca1..b619557b96 100644 --- a/includes/db/DatabasePostgres.php +++ b/includes/db/DatabasePostgres.php @@ -6,7 +6,7 @@ * @ingroup Database */ -class PostgresField { +class PostgresField implements Field { private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname; static function fromText($db, $table, $field) { @@ -69,7 +69,7 @@ SQL; return $this->type; } - function nullable() { + function isNullable() { return $this->nullable; } diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 1ebf1fdb8a..4635d3384c 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -626,7 +626,7 @@ class DatabaseSqliteStandalone extends DatabaseSqlite { /** * @ingroup Database */ -class SQLiteField { +class SQLiteField implements Field { private $info, $tableName; function __construct( $info, $tableName ) { $this->info = $info; @@ -651,17 +651,10 @@ class SQLiteField { return $this->info->dflt_value; } - function maxLength() { - return -1; - } - - function nullable() { + function isNullable() { return !$this->info->notnull; } - # isKey(), isMultipleKey() not implemented, MySQL-specific concept. - # Suggest removal from base class [TS] - function type() { return $this->info->type; } diff --git a/includes/installer/MysqlUpdater.php b/includes/installer/MysqlUpdater.php index b8414dec13..50376a5ca4 100644 --- a/includes/installer/MysqlUpdater.php +++ b/includes/installer/MysqlUpdater.php @@ -601,7 +601,7 @@ class MysqlUpdater extends DatabaseUpdater { */ protected function doWatchlistNull() { $info = $this->db->fieldInfo( 'watchlist', 'wl_notificationtimestamp' ); - if ( $info->nullable() ) { + if ( $info->isNullable() ) { $this->output( "...wl_notificationtimestamp is already nullable.\n" ); return; } diff --git a/includes/installer/PostgresUpdater.php b/includes/installer/PostgresUpdater.php index ed57098681..44f5a245fe 100644 --- a/includes/installer/PostgresUpdater.php +++ b/includes/installer/PostgresUpdater.php @@ -442,7 +442,7 @@ END; $this->output( "... error: expected column $table.$field to exist\n" ); exit( 1 ); } - if ( $fi->nullable() ) { + if ( $fi->isNullable() ) { # # It's NULL - does it need to be NOT NULL? if ( 'NOT NULL' === $null ) { $this->output( "Changing \"$table.$field\" to not allow NULLs\n" ); @@ -616,7 +616,7 @@ END; protected function checkRcCurIdNullable(){ $fi = $this->db->fieldInfo( 'recentchanges', 'rc_cur_id' ); - if ( !$fi->nullable() ) { + if ( !$fi->isNullable() ) { $this->output( "Removing NOT NULL constraint from \"recentchanges.rc_cur_id\"\n" ); $this->applyPatch( 'patch-rc_cur_id-not-null.sql' ); } else { -- 2.20.1